#include "pool.hpp"

namespace sdl_engine::component::object_pool {

void GCObject::DelSelf() {
    valid_ = false;
}

void GCObject::ImDelSelf() {
    valid_ = false;
    pool_->Delete(this);
}


GCObjectPool::GCObjectPool(size_t reserve) {
    pool_.reserve(reserve);
}

int GCObjectPool::findEmptySlot() {
    int idx = 0;
    while (pool_.find(idx) != pool_.end()) idx++;
    return idx;
}

void GCObjectPool::Gc() {
    auto it = findInvalidSlot();
    while (it != pool_.end()) {
        pool_.erase(it);
        it = findInvalidSlot();
    }
}

GCObjectPool::PoolType::iterator GCObjectPool::findInvalidSlot() {
    return std::find_if(pool_.begin(), pool_.end(),
                        [](const std::pair<const int, std::unique_ptr<GCObject>>& e) {
                            return !e.second->valid_;
                        });
}

}
